package com.zy.cat.common.utils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.util.DigestUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.taobao.api.internal.util.WebUtils;
import com.zy.cat.entity.ztk.resp.ZTKShopDetail;

import lombok.extern.slf4j.Slf4j;

/**
 * 折淘客接口获取信息
 * 
 * @author Link
 * @date 2019-06-28 14:15
 */
@Slf4j
public class ZTKAPI {
	//private String appkey = "12a961d38138489cae15e2aaee1178cd";
	//private String sid = "22982";
	private String appkey = "9b35cd52f6114c99bae130bf36d47b3a";
	private String sid = "57362";

	private ZTKAPI(String appkey, String sid) {
		super();
		this.appkey = appkey;
		this.sid = sid;
	}

	/**
	 * 
	 * @author Link
	 * @date 2019-06-28 08:53
	 * @param type 0.zjcails1314（默认） 
	 * @return
	 */
	public static ZTKAPI getInstance(int type) {
		String appkey = "9b35cd52f6114c99bae130bf36d47b3a";
		String sid = "57362";
		
		return new ZTKAPI(appkey, sid);
	}

	public ZTKShopDetail getShopDetail(String sid) {
		ZTKShopDetail result=null;
		String url = "https://api.zhetaoke.com:10002/api/api_detail.ashx";
		String resultJson = "";
		Map<String, String> props = new HashMap<String, String>();
		props.put("appkey", appkey);
		props.put("tao_id", sid);
//		props.put("code", "1");
		try {
			resultJson = WebUtils.doGet(url, props);
		} catch (IOException e) {
			return null;
		}
		if(StringUtils.isNotBlank(resultJson)&&JSON.isValidObject(resultJson)) {
			result=JSON.parseObject(resultJson, ZTKShopDetail.class);
		}
		return result;
	}
	
	/**
	 * 
	 * @info 猜你喜欢
	 * @author Link
	 * @date 2019-11-21 19:21
	 * @param sort	
	 * <p>商品排序方式</p>
		<p>new：按照综合排序</p>
		<p>total_sale_num_asc：按照总销量从小到大排序</p>
		<p>total_sale_num_desc：按照总销量从大到小排序</p>
		<p>sale_num_asc：按照月销量从小到大排序</p>
		<p>sale_num_desc：按照月销量从大到小排序</p>
		<p>commission_rate_asc：按照佣金比例从小到大排序</p>
		<p>commission_rate_desc：按照佣金比例从大到小排序</p>
		<p>price_asc：按照价格从小到大排序</p>
		<p>price_desc：按照价格从大到小排序</p>
		<p>coupon_info_money_asc：按照优惠券金额从小到大排序</p>
		<p>coupon_info_money_desc：按照优惠券金额从大到小排序</p>
		<p>shop_level_asc：按照店铺等级从低到高排序</p>
		<p>shop_level_desc：按照店铺等级从高到低排序</p>
		<p>tkfee_asc：按照返佣金额从低到高排序</p>
		<p>tkfee_desc：按照返佣金额从高到低排序</p>
		<p>code：按照code值从大到小排序</p>
		<p>date_time：按照更新时间排序</p>
		<p>random：按照随机排序</p>
	 * @param sid	商品ID：用户商品关联推荐
	 * @param imei	手机IMEI
	 * @param idfa	IOS手机IDFA
	 * @param page	分页
	 * @param pageSize	每页大小默认40
	 * @return
	 */
	public ZTKShopDetail getShopLike(String sort,String sid,String imei,String idfa ,int page,int pageSize) {
		page=page>0?page:1;
		pageSize=pageSize>0?pageSize:40;
		String url = "https://api.zhetaoke.com:10001/api/open_item_guess_like.ashx";
		String result = "";
		Map<String, String> props = new HashMap<String, String>();
		props.put("appkey", appkey);
		props.put("page_size", ""+pageSize);
		props.put("page", ""+page);
		props.put("sort",sort);
		if(StringUtils.isNotBlank(sid)) {
			props.put("item_id", sid);
		}else if(StringUtils.isNotBlank(imei)) {
			props.put("device_encrypt","MD5");
			props.put("device_value", DigestUtils.md5DigestAsHex(("" + imei).getBytes()));
			props.put("device_type", "IMEI");
		}else if(StringUtils.isNotBlank(idfa)) {
			props.put("device_encrypt","MD5");
			props.put("device_value", DigestUtils.md5DigestAsHex(("" + idfa).getBytes()));
			props.put("device_type", "IDFA");
		}
		try {
			log.info(url+"?"+WebUtils.buildQuery(props, "utf-8"));
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		try {
			result = WebUtils.doGet(url, props,"UTF-8");
//			log.info("-getShopLike-->{}",result);
		} catch (IOException e) {
			return null;
		}
		ZTKShopDetail shopDetail = null; 
		if(JSONObject.isValid(result)) {
			shopDetail = JSON.parseObject(result, ZTKShopDetail.class);
			if(shopDetail==null||shopDetail.getStatus()!=200) {
				log.error("getShopLike-Error-->{}",result);
				return null;
			}
		}
																
		return shopDetail;
	}
	
	public static void main(String[] args) {
		ZTKAPI ztkapi = getInstance(0);
		ZTKShopDetail shopLike = ztkapi.getShopLike("total_sale_num_asc", "", "861520049706059", "", 1, 10);
		System.out.println(JSON.toJSONString(shopLike));
	}
}
